mod physics;
mod visual;
mod mover;
mod balloon;
mod spring;

use crate::mover::Mover;
use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};


pub struct State{
    balloon: balloon::Balloon,
    spring: spring::Spring
}

impl State{
    pub fn new(_ctx: &mut Context) -> State {
        let location = Vec2{x: 100.0,y: 400.0};
        let balloon = balloon::Balloon::new(location);
        let anchor = Vec2{x: 400.0, y: 0.0};
        let len = 200.0;
        let spring = spring::Spring::new(anchor, len);
        State{ balloon, spring }
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, ctx: &mut Context) -> GameResult {
        let fg = Vec2 {x: 0.0, y: 0.1*self.balloon.get_mass()};
        self.balloon.apply_force(fg);
        
        self.balloon.update(ctx);
        self.spring.connect(&mut self.balloon);
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);
        self.spring.display(ctx, &mut canvas);
        self.balloon.display(ctx, &mut canvas);
        canvas.finish(ctx)?;
        Ok(())
    }
}